<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="工厂">
          <el-select
            v-model="searchParam.plantId"
            placeholder="请输入工厂"
            clearable
            @keyup.enter="search"
          >
            <el-option
              v-for="item in plantList"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="index"
          label="序号"
          width="70"
          key="id"
          align="center"
        />
        <el-table-column
          label="工厂"
          prop="plantCode"
          width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            <span v-show="scope.row.plantCode">
              {{ `${scope.row.plantCode}` }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="工作中心"
          prop="workCenterCode"
          min-width="150"
          show-overflow-tooltip
        >
          <template #default="scope">
            <span v-show="scope.row.workCenterCode">
              {{ `${scope.row.workCenterCode} - ${scope.row.workCenterName}` }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="物料"
          prop="materialCode"
          min-width="150"
          show-overflow-tooltip
        >
          <template #default="scope">
            <span v-show="scope.row.materialCode">
              {{ `${scope.row.materialCode} - ${scope.row.materialName}` }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="总数量"
          prop="totalQuantity"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="入库时间"
          prop="stockageRefreshTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="报警时间"
          prop="callPoliceTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="业务组"
          prop="workOrganizationName"
          min-width="100"
          show-overflow-tooltip
        />
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <meswarehousecallinfo-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESMESWAREHOUSECALLINFO",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/meswarehousecallinfo/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  plantId: "",
};

const deleteParam = {
  api: "/mes/meswarehousecallinfo/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getPlantList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/meswarehousecallinfo/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开仓库报警记录表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改仓库报警记录";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增仓库报警记录";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开仓库报警记录表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "仓库报警记录详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
//工厂列表
const plantList = ref([]);
const getPlantList = async () => {
  const { data } = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plantList.value = data;
  searchParam.value.plantId = plantList.value[0].id;
};
</script>
<style lang="scss" scoped>
:deep(.el-form .el-form-item__label) {
  width: 35px;
}
</style>
